// Storage.cpp: implementation of the CStorage class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Storage.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CStorage::CStorage()
:m_hKey(NULL)
{
}

CStorage::CStorage(LPCSTR sSection)
:m_hKey(NULL)
{
  char s[500]="SOFTWARE\\DeeP125\\CoodTimer\\";
  strcat(s,sSection);
  LONG ret=RegCreateKeyEx(HKEY_CURRENT_USER,s,0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&m_hKey,NULL);
}

CStorage::~CStorage()
{
  if(m_hKey)
    RegCloseKey(m_hKey);

}
void CStorage::Get(LPCSTR sName,LPCSTR sDefault,LPSTR sVal,int cb) const
{
  strcpy(sVal,sDefault);
  DWORD dwType=REG_SZ;
  DWORD dwSize=cb;
  RegQueryValueEx(m_hKey,sName,NULL,&dwType,(BYTE*)sVal,&dwSize);
}
void CStorage::Put(LPCSTR sName,LPCSTR sVal)
{
  if(sVal==NULL || *sVal==0)
    RegDeleteValue(m_hKey,sName);
  else
    RegSetValueEx(m_hKey,sName,0,REG_SZ,(BYTE*)sVal,strlen(sVal)+1);

}
int  CStorage::Get(LPCSTR sName,int nDefault) const
{
  DWORD dw=nDefault;
  DWORD dwType=REG_DWORD;
  DWORD dwSize=sizeof(dw);
  RegQueryValueEx(m_hKey,sName,NULL,&dwType,(BYTE*)&dw,&dwSize);
  return dw;
}
void CStorage::Put(LPCSTR sName,int nVal)
{
  RegSetValueEx(m_hKey,sName,0,REG_DWORD,(BYTE*)&nVal,sizeof(nVal));
}
void CStorage::GetCombo(LPCSTR sName,HWND hWnd,int MaxCount) const
{
 char name[120];
 char s[256];
 for(int i=0;i<MaxCount;i++)
  {
    wsprintf(name,"%s%d",sName,i);
    Get(name,"",s,sizeof(s));
    if(s[0]==0)
      break;
    if(i==0)
     SetWindowText(hWnd,s);
     SendMessage(hWnd,CB_ADDSTRING,0,(long)s);
  }
}
void CStorage::PutCombo(LPCSTR sName,HWND hWnd,int MaxCount)
{
  char s[256];
  char s2[256];
  GetWindowText(hWnd,s,sizeof(s));
  char name[120];
  wsprintf(name,"%s0",sName);
  Put(name,s);
  int nCount=SendMessage(hWnd,CB_GETCOUNT,0,0);
  for(int i=0,j=1;i<nCount;i++)
  {
   if(j>=MaxCount)
     break;
   SendMessage(hWnd,CB_GETLBTEXT,i,(long)s2);
   if(strcmp(s2,s)!=0)
   {
      wsprintf(name,"%s%d",sName,j++);
      Put(name,s2);
   }
  }
}
